# # 打开文件
# f = open('/Users/zhangguoqiang/PycharmProjects/pythonProject/fileDaemon/test.txt', 'r', encoding="UTF-8")
# print(f"f的文件类型为:{type(f)}")
#
# # 读取文件-read()
# print(f"读取10个字节的结果是:{f.read(10)}")
# print(f"read方法读取全部内容的结果是:{f.read()}")
# # 在程序中连续调用2次read，第二次调用会在第一次调用结束的位置接着读取
# print("---------------------------------------------------")

# 读取文件-readlines——读取文件的全部行，封装到列表中
# lines = f.readlines()
# print(f"lines对象的类型是:{type(lines)}")
# print(f"lines对象的内容是:{lines}")

# 读取文件——readline()
# line1 = f.readline()
# line2 = f.readline()
# line3 = f.readline()
# print(f"第一行数据:{line1}")
# print(f"第一行数据:{line2}")
# print(f"第一行数据:{line3}")

# for循环读取文件行
# for line in f:
#     print(f"每一行数据是:{line}")

# try:
#     print(1)
# except Exception as e:
#     print(e)
# else:
#     print('我是else，是没有异常的时候执行的代码')


try:
    f = open('/Users/zhangguoqiang/PycharmProjects/pythonProject/fileDaemon/world.txt', 'r', encoding="UTF-8")
except Exception as e:
    f = open('/Users/zhangguoqiang/PycharmProjects/pythonProject/fileDaemon/world.txt', 'w', encoding="UTF-8")
else:
    print('没有异常')
finally:
    f.close()